package com.a01_2021.thousandstimes.instance.service.impl;

import com.a01_2021.thousandstimes.exception.CustomException;
import com.a01_2021.thousandstimes.exception.CustomExceptionType;
import com.a01_2021.thousandstimes.instance.entity.LearnToday;
import com.a01_2021.thousandstimes.instance.mapper.LearnTodayMapper;
import com.a01_2021.thousandstimes.instance.service.ILearnTodayService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author E1xP
 * @since 2021-03-28
 */
@Service
public class LearnTodayServiceImpl extends ServiceImpl<LearnTodayMapper, LearnToday> implements ILearnTodayService {

    @Resource
    LearnTodayMapper learnTodayMapper;

    /**
     * 添加今日学日志
     *
     * @param userId  用户id
     * @param content 今日学内容
     * @return 返还添加今日学实体类
     */
    @Override
    public LearnToday add(long userId, String content) {
        LearnToday learnToday = new LearnToday(null, userId, content, LocalDateTime.now());
        learnTodayMapper.insert(learnToday);
        return learnToday;
    }


    /**
     * 查询用户全部的今日学
     *
     * @param userId 用户id
     * @return 今日学列表
     */
    @Override
    public List<LearnToday> get(long userId) {
        QueryWrapper<LearnToday> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        return learnTodayMapper.selectList(queryWrapper);
    }

    /**
     * 更新用户的今日学
     *
     * @param userId     用户id
     * @param learnToday 更新的今日学实体
     * @return 更新后的今日学实体
     * @throws CustomException 当今日学不存在，该今日学不属于该用户是抛出该异常
     */
    @Override
    public LearnToday update(long userId, LearnToday learnToday) throws CustomException {
        LearnToday learnTodayDate = learnTodayMapper.selectById(learnToday);
        if (learnTodayDate == null) {
            throw new CustomException(CustomExceptionType.LEARN_TODAY_NOT_FOUND_ERROR, "找不到该今日学");
        }
        if (learnTodayDate.getUserId().equals(userId)) {
            throw new CustomException(CustomExceptionType.OPERATION_NOT_ALLOW, "不允许修改他人的今日学内容");
        }
        learnTodayDate.setContent(learnToday.getContent());
        learnTodayMapper.updateById(learnTodayDate);
        return learnTodayDate;
    }

    /**
     * 通过用户id与月份时间获取当月用户的今日学
     *
     * @param userId 用户id
     * @param date   月份时间
     * @return List今日学列表
     */
    @Override
    public List<LearnToday> getFromUserIdAndDate(long userId, LocalDate date) {
        LocalDate startDate = date.minusDays(date.getDayOfMonth() - 1);
        LocalDate endTime = date.plusMonths(1);
        QueryWrapper<LearnToday> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        queryWrapper.between("log_time", startDate, endTime);
        return learnTodayMapper.selectList(queryWrapper);
    }
}
